package com.cj.http;

import com.cj.Mapper.BooksMapper;
import com.cj.Mapper.ReviewMapper;
import com.cj.models.BookModel;
import com.cj.models.BookReviews;
import com.cj.models.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping(path = "/review")
public class ReviewForPost {

    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private BooksMapper booksMapper;

    @PostMapping(path = "{bookid:\\d+}")
    public ResultModel add(@PathVariable("bookid") int bookid,
                           @RequestBody BookReviews bookReviews) throws Exception {
        BookModel bookd = booksMapper.selectById(bookid);
        if(bookd==null || bookd.getBookId()<=0){
            throw new Exception("not book");
        }
        bookReviews.setBookId(bookid);
        bookReviews.setRDate(new Date());
        int ret = reviewMapper.insert(bookReviews);
        if(ret!=1){
            return new ResultModel("success","0");
        }
        return new ResultModel("success",1);
    }
}
